import { setTimeout } from 'core-js';
/**
 * 常用工具
 * @author maybe
 */

/**
 * 存储localStorage
 */
export const setStore = (name, content) => {
  if (!name) return;
  if (typeof content !== 'string') {
    content = JSON.stringify(content);
  }
  window.localStorage.setItem(name, content);
};
/**
 * 获取localStorage
 */
export const getStore = (name) => {
  if (!name) return;
  return window.localStorage.getItem(name);
};
/**
 * 删除localStorage
 */
export const removeStore = (name) => {
  if (!name) return;
  window.localStorage.removeItem(name);
};
/**
 * 延迟加载方法
 * @param {*} fn
 * @param {*} time
 */
export const submitTimeOut = (fn, time = 1000) => {
  setTimeout(function () {
    fn();
  }, time);
};

/**
 * 深拷贝
 * @param {*} obj 
 */
export const deepClone = (obj) => {
  var result = Array.isArray(obj) ? [] : {};
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
      if (typeof obj[key] === 'object') {
        result[key] = deepClone(obj[key]);   //递归复制
      } else {
        result[key] = obj[key];
      }
    }
  }
  return result;
}
/**
 * 函数节流
 * @param {*} fn 
 * @param {*} interval 
 */
export const throttle = (fn, interval = 300, isInit) => {
  let canRun = true;
  if (isInit) fn();
  return function () {
    if (!canRun) return;
    canRun = false;
    setTimeout(() => {
      fn.apply(this, arguments);
      canRun = true;
    }, interval);
  };
}
/**
 * 函数防抖 (只执行最后一次点击)
 * @param fn
 * @param delay
 * @returns {Function}
 * @constructor
 */
export const debounce = (fn, t) => {
  let delay = t || 500;
  let timer;
  // console.log(fn)
  // console.log(typeof fn)
  return function () {
    let args = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      timer = null;
      fn.apply(this, args);
    }, delay);
  }
};
/**
 * 数字转整数 如 100000 转为10万
 * @param {需要转化的数} num 
 * @param {需要保留的小数位数} point 
 */
export const tranNumber = (num, point) => {
  let numStr = parseInt(num).toString()
  // 十万以内直接返回 
  if (numStr.length < 6) {
    return numStr;
  }
  //大于8位数是亿
  else if (numStr.length > 8) {
    let decimal = numStr.substring(numStr.length - 8, numStr.length - 8 + point);
    return parseFloat(parseInt(num / 100000000) + '.' + decimal) + '亿';
  }
  //大于6位数是十万 (以10W分割 10W以下全部显示)
  else if (numStr.length > 5) {
    let decimal = numStr.substring(numStr.length - 4, numStr.length - 4 + point)
    return parseFloat(parseInt(num / 10000) + '.' + decimal) + '万';
  }
}
/**
 * 使用<a>标签下载文件
 * @param url 下载地址
 */
export const download = url => {
  if (!url) {
    return
  }
  // var blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
  // var url = window.URL.createObjectURL(blob);
  var aLink = document.createElement('a')
  aLink.style.display = 'none'
  aLink.href = url
  // aLink.setAttribute("download", "excel.xls");
  document.body.appendChild(aLink)
  aLink.click()
  document.body.removeChild(aLink) //下载完成移除元素
  // window.URL.revokeObjectURL(url); //释放掉blob对象
}

// export const nestTimeOut = (fn, interval) => {
//   let stopFlag = false;
//   setTimeout(() => {
//     if (!stopFlag) {
//       fn();
//       setTimeout(fn, interval);
//     }
//   }, interval)

// }

export const isMobile = () => {
  var userAgentInfo = navigator.userAgent;
  var Agents = ["Android", "iPhone",
    "SymbianOS", "Windows Phone",
    "iPad", "iPod"];
  var flag = false;
  for (var v = 0; v < Agents.length; v++) {
    if (userAgentInfo.indexOf(Agents[v]) > 0) {
      flag = true;
      break;
    }
  }
  return flag;
}
/**
 * 基于setInterval的带回调函数的倒计时工具函数
 * @param {Number} during 倒计时总时间 /秒
 * @param {Number} interval 倒计时间隔 /秒
 * @param {Function} intervalCB 间隔callback
 * @param {Function} finishCB 倒计时结束callback
 */
//倒计时
export const countDown = (long, back) => {
  var timer = null,
    changeTime = 0;
  if (timer) {
    clearInterval(timer);
  }
  changeTime = long;
  back(changeTime);
  timer = setInterval(function () {
    changeTime--;
    back(changeTime);
    if (changeTime < 1) {
      clearInterval(timer);
    }
  }, 1000);
}
/**
 * 判断是否是微信浏览器
 */
export const isWeiXin = () => {
  var ua = window.navigator.userAgent.toLowerCase();
  if (ua.match(/MicroMessenger/i) == 'micromessenger') {
    return true;
  } else {
    return false;
  }
}

/**
* 清除响应式数据
* @author Maybe
* @time 2019年8月29日 09:54:31
* @param {Object}  form 重置的数据对象
*/
export const clearForm = (form) => {
  let keys = Object.keys(form);
  keys.map((item, index) => {
    if (Array.isArray(form[index])) {
      form[item].length = 0;
    } else {
      form[item] = null;
    }
  })
}
/**
 * 导出流数据
 * @author Maybe
 * @param {*} data 
 * @param {*} name 
 */
export const exportStreamData = (data, name) => {
  let content = data;
  let blob = new Blob([content])
  let fileName = name;
  if ('download' in document.createElement('a')) { // 非IE下载
    let elink = document.createElement('a')
    elink.download = fileName
    elink.style.display = 'none'
    elink.href = URL.createObjectURL(blob)
    document.body.appendChild(elink)
    elink.click()
    URL.revokeObjectURL(elink.href) // 释放URL 对象
    document.body.removeChild(elink)
  } else { // IE10+下载
    navigator.msSaveBlob(blob, fileName)
  }
}
/**
 * 判断对象属性是否为空
 * @param {*} obj 
 */
export const isNull = (obj) => {
  return Object.keys(obj).length > 0;
}